package com.controller;

import com.service.ChartsService;
import com.vo.ChartsVo;
import com.vo.PieChartsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/12/7 - 14:09
 */
@RestController
@RequestMapping("/charts")
public class ChartsController {
    @Autowired
    private ChartsService chartsService;

    /**
     * 统计每年营业额
     *
     * @return
     */
    @RequestMapping("/getHotelYearCharts")
    public Map<String, Object> getHotelYearCharts() {
        //返回至前台的map
        Map<String, Object> maps = new HashMap<String, Object>();
        List<Map> list = chartsService.getTotalPriceByYear();
        //创建两个List集合，分别保存年份及对应的营业额
        List<String> keyList = new ArrayList<String>();//年份
        List<Double> valueList = new ArrayList<Double>();//营业额
        //遍历
        for (Map map : list) {
            //年份集合
            keyList.add(map.get("YEAR").toString());
            //营业额集合
            valueList.add(Double.valueOf(map.get("totalPrice").toString()));
        }
        maps.put("keyList", keyList);
        maps.put("valueList", valueList);
        return maps;
    }

    /**
     * 统计月营业额报表
     *
     * @return
     */
    @RequestMapping("/getHotelYearOfMonthCharts")
    public List<Double> getHotelYearOfMonthCharts(String year) {
        List<Double> list = chartsService.getTotalPriceByYearOfMonth(year);
        for (int i = 0; i < list.size(); i++) {
            //判断当前月份是否有数据，如果没有数据则以0代替
            if (list.get(i) == null) {
                list.set(i, 0D);
            }
        }
        return list;
    }

    /**
     * 根据指定的年份统计该年份下每个季度的营业额
     *
     * @param year
     * @return
     */
    @RequestMapping("/getQuarterTotalPriceByYear")
    public Map<String, Object> getQuarterTotalPriceByYear(String year) {
        //返回给页面的map
        Map<String, Object> maps = new HashMap<String, Object>();
        List<Map> quarterTotalPriceList = chartsService.getQuarterTotalPriceByYear(year);
        //保存房间类型
        List<String> keyList = new ArrayList<String>();
        //保存季度营业额
        List<ChartsVo> datas = new ArrayList<ChartsVo>();
        for (Map name : quarterTotalPriceList) {
            //房间类型
            String typeName = name.get("typeName").toString();
            keyList.add(typeName);
            //季度营业额数组
            Double[] dataList = {
                    Double.valueOf(name.get("q1").toString()),
                    Double.valueOf(name.get("q2").toString()),
                    Double.valueOf(name.get("q3").toString()),
                    Double.valueOf(name.get("q4").toString())
            };
            ChartsVo chartsVo = new ChartsVo(typeName, "line", "总营业额", dataList);
            datas.add(chartsVo);
        }
        maps.put("keyList", keyList);
        maps.put("valueList", datas);
        return maps;
    }

    /**
     * 根据年份和月份查询房间类型总数量
     *
     * @param selectDate
     * @return
     */
    @RequestMapping("/getRoomTypeCountByYearAndMonth")
    public List<PieChartsEntity> getRoomTypeCountByYearAndMonth(String selectDate) {
        return chartsService.getRoomTypeCountByYearAndMonth(selectDate);
    }

    /**
     * 查询当天入住的记录 （饼图所需数据）
     *
     * @return
     */
    @RequestMapping("/getCurrentCheckinCharts")
    public List<PieChartsEntity> getCurrentCheckinCharts(String selectDate) {
        return chartsService.getCurrentCheckinCharts(selectDate);
    }
}
